package com.aida.evil.eda.fragment;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Adapter.CommonAdapter;
import com.aida.evil.eda.Adapter.ViewHolder;
import com.aida.evil.eda.Answer.AnswerActivity;
import com.aida.evil.eda.Data.NetworkData;
import com.aida.evil.eda.Data.UserData;
import com.aida.evil.eda.Volley.VolleyHandler;
import com.aida.evil.eda.Volley.VolleyHttpRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

/********************************************
 * 工程名：Aida
 * 包名：com.aida.evil.eda.fragment
 * 时间：16-5-20 下午12:46
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：继承于Share，用于补充Share中的流量兑换记录
 ********************************************/
public class Share_FlowRecord extends Share
{
    private static final String FLOWRECORDURL = "http://" + NetworkData.ServiceIP + "/FlowRecord?";

    private ListView listView;/*底部的积分兑换记录*/
    private TextView hint_TextView;/*暂时没有积分兑换记录    提示用TextView*/
    private ProgressBar progressBar;

    private Context context;
    private View view;
    private Adapter adapter;


    private ArrayList<Share_FlowRecord_Bean> list = new ArrayList<>();

    /**
     * 进行加载
     */
    public static void load(View view, Context context)
    {
        Share_FlowRecord share_flowRecord = new Share_FlowRecord();

        share_flowRecord.context = context;
        share_flowRecord.view = view;
        share_flowRecord.initView();/*初始化控件*/
        share_flowRecord.flowRecord_toNetWork();/*网络加载*/
    }

    /**
     * 流量记录网络加载
     */
    private void flowRecord_toNetWork()
    {
        VolleyHandler<String> stringVolleyHandler = new VolleyHandler<String>()
        {
            ItemList itemList;

            @Override
            public void reqSuccess(String response)
            {
                Log.i("流量记录网络加载", response);
                progressBar.setVisibility(View.GONE);
                /*解析JSON*/
                {
                    Type type = new TypeToken<ItemList>()
                    {
                    }.getType();
                    itemList = new Gson().fromJson(response, type);
                }
                /*解析数据*/
                {
                    if (itemList.size == 0)
                    {
                        hint_TextView.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        listView.setVisibility(View.VISIBLE);
                        analysisData(itemList);/*处理数据*/
                    }
                }
            }

            @Override
            public void reqError(String error)
            {
                progressBar.setVisibility(View.GONE);
                hint_TextView.setVisibility(View.VISIBLE);
            }
        };

        Map<String, String> map = new HashMap<>();
        map.put("userPhoneNum", UserData.DATA_PHONENUM);
        VolleyHttpRequest.String_request(FLOWRECORDURL, map, stringVolleyHandler);
    }

    /**
     * 处理数据
     */
    private void analysisData(ItemList itemList)
    {
        ArrayList<ItemBean> itemBeanArrayList = itemList.list;
        for (int i = 0; i < itemList.size; i++)
        {
            ItemBean itemBean = itemBeanArrayList.get(i);
            list.add(new Share_FlowRecord_Bean
                    (itemList.userPhoneNum, itemBean.flow
                            , itemBean.datetime, itemBean.failure));
        }
        setListView();/*设置ListView*/
    }

    /**
     * 设置ListView
     */
    private void setListView()
    {
        adapter = new Adapter(context, list, R.layout.mainactivity_fragment_share_listview);
        listView.setAdapter(adapter);
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        listView = (ListView) view.findViewById(R.id.mainactivity_fragment_share_listView);
        hint_TextView = (TextView) view.findViewById(R.id.mainactivity_fragment_share_hint);
        progressBar = (ProgressBar) view.findViewById(R.id.mainactivity_fragment_share_progressBar);
    }

    /*************************************
     * 内部类
     */

    private class Adapter extends CommonAdapter<Share_FlowRecord_Bean>
    {

        public Adapter(Context context, List<Share_FlowRecord_Bean> itemBean_list, int itemLayoutResId)
        {
            super(context, itemBean_list, itemLayoutResId);
        }

        @Override
        public void convert(ViewHolder viewHolder, Share_FlowRecord_Bean item)
        {
            TextView userPhoneNum_TextView = viewHolder.getView(R.id.mainactivity_fragment_share_listView_userPhoneNum);
            TextView flow_TextView = viewHolder.getView(R.id.mainactivity_fragment_share_listView_flow);
            TextView datetime_TextView = viewHolder.getView(R.id.mainactivity_fragment_share_listView_datetime);
            TextView failure_TextView = viewHolder.getView(R.id.mainactivity_fragment_share_listView_failure);

            userPhoneNum_TextView.setText("手机号：" + item.userPhoneNum);
            flow_TextView.setText("已兑换的流量：" + item.flow);
            datetime_TextView.setText("兑换时间：" + item.datetime);
            if (item.failure.equals("") || item.failure.equals("0"))
                failure_TextView.setText("状态：成功");
            else
                failure_TextView.setText("状态：" + item.failure);
        }
    }

    private class Share_FlowRecord_Bean
    {
        private String userPhoneNum;
        private int flow;
        private String datetime;
        private String failure;

        public Share_FlowRecord_Bean(String userPhoneNum, int flow, String datetime, String failure)
        {
            this.userPhoneNum = userPhoneNum;
            this.flow = flow;
            this.datetime = datetime;
            this.failure = failure;
        }
    }

    private class ItemList
    {
        private String userPhoneNum;
        private int size;
        private ArrayList<ItemBean> list;
    }

    private class ItemBean
    {
        private int flow;
        private String datetime;
        private String failure;
    }
}
